package com.sws.worktive.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.sws.worktive.R;
import com.sws.worktive.bitmaploading.ImageLoader;
import com.sws.worktive.job_service.JobListJsonParser.JobData;

public class JobListAdapter extends BaseAdapter{
				
	private ArrayList<JobData> jobDatas;
	
	private Context mContext;
	  
	ImageLoader imageLoader;
 
	public JobListAdapter(Context context){
		this(context, new ArrayList<JobData>());
	}
	
	public JobListAdapter(Context context, ArrayList<JobData> jobDatas) {
		this.jobDatas = jobDatas;
		mContext = context;
		imageLoader = new ImageLoader(context);
	}

	@Override
	public int getCount() {
		return jobDatas.size();
	}

	@Override
	public Object getItem(int arg0) {
		return jobDatas.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	public void addJObs(JobData jobData) {
		jobDatas.add(jobData);
	}
	
	public ArrayList<JobData> getJobDatas(){
		return jobDatas;
	}
	
	public JobListAdapter clearList(){
		jobDatas.clear();
		return this;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		JobData jobData = (JobData)getItem(position);
		
		ViewHolder viewHolder;
		
//		TODO: for some reason some reason some reason. If I use the viewholder like this, The picture is not get set properly...
//		if (convertView == null) {
			LayoutInflater layoutInflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = layoutInflater.inflate(R.layout.view_row, parent, false);
			
			viewHolder = new ViewHolder();
			viewHolder.imgPerson = (ImageView) convertView.findViewById(R.id.img_person);
			viewHolder.txtName = (TextView) convertView.findViewById(R.id.txt_personName);
			viewHolder.txtDesignation = (TextView) convertView.findViewById(R.id.txt_personDesignation);
			viewHolder.txtCountry = (TextView) convertView.findViewById(R.id.txt_personCountry);
			viewHolder.txtJobType = (TextView) convertView.findViewById(R.id.txt_personJobType);
			viewHolder.txtDateAdded = (TextView) convertView.findViewById(R.id.txt_dateadded);
			viewHolder.txtDateModified = (TextView) convertView.findViewById(R.id.txt_lastmodified);
			viewHolder.txtDescription = (TextView) convertView.findViewById(R.id.txt_description);
			viewHolder.imgJobPic = (ImageView) convertView.findViewById(R.id.img_jobPic);

			convertView.setTag(viewHolder);
//		} else {
//			viewHolder = (ViewHolder) convertView.getTag();
//		}
		if (jobData.isIamage.equals("0")) {
			viewHolder.txtDescription.setVisibility(View.VISIBLE);
			viewHolder.txtDescription.setText(jobData.description);
			viewHolder.imgPerson.setBackgroundResource(R.drawable.pro_pic);
		} else if (jobData.isIamage.equals("1")) {
			Picasso.with(mContext).load(jobData.imageThumbnail).fit().into(viewHolder.imgPerson, new Callback() {
				@Override
				public void onSuccess() {
				}
				
				@Override
				public void onError() {
				}
			});
		}
		viewHolder.txtCountry.setText(jobData.city);
		viewHolder.txtJobType.setText(" |" + jobData.jobtype);
		viewHolder.txtDateAdded.setText(" " + jobData.addedDate + splitString(jobData.addedDate));
		viewHolder.txtDateModified.setText(" " + splitString(jobData.lastModDate));
		viewHolder.imgPerson.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			}
		});
		return convertView;
	}

	String splitString(String str) {
		if (str != null) {

			String[] tokens = str.split(" ");
			return tokens[0];
		}
		return "";
	}

	class ViewHolder {

		ImageView imgPerson, imgJobPic;
		TextView txtName, txtDesignation, txtCountry, txtJobType,
				txtDateAdded, txtDateModified, txtDescription, txtNoLike,
				txtNoComment;
	}

}
